package com.bilin.coin.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.utils.StatusBarUtil;

import butterknife.BindView;
import kt.com.bilin.coin.common.ARouteConfigKt;
import kt.com.bilin.coin.ui.activity.SimpleVideoViewPlayerActivity;
import kt.com.bilin.coin.utils.Logger;


/**
 * Created by LG on 2017/3/18.
 */
@Route(path = "/common/WebPageLoading")
public class WebPageLoadingActivity extends BaseAppCompatActivity {


    @BindView(R.id.pb_loading)
    ProgressBar pbLoading;
    @BindView(R.id.wv_details)
    WebView wvDetails;

    @Autowired
    String title;

    @Autowired
    String url;

    @Autowired
    int type;//0：title原来的样子 1:title变白 状态栏也变白
    @Autowired
    String menuName;
    @Autowired
    String menuUrl;
    @Autowired
    boolean isVideo;

    @Override
    protected void initView() {
        setTitle(title);
        if (type == 1) {
            //根据状态栏颜色来决定状态栏文字用黑色还是白色
            StatusBarUtil.setStatusBarMode(WebPageLoadingActivity.this, true, R.color.colorWhiteMain);
            //mToolbar.setNavigationIcon(R.drawable.ic_chevron_left_black2_24dp);
            //mToolbar.setBackgroundColor(getResources().getColor(R.color.colorWhiteMain));
            mTvTitle.setTextColor(getResources().getColor(R.color.black333));
        }
        if (menuName != null) {
            setMenu(menuName);
        }
    }

    @Override
    public void setMenuOnClickListener() {
        if (menuName != null) {
            if (isVideo) {
                baseStartActivityWith(ARouteConfigKt.PATH_SIMPLE_VIDEO).withString(SimpleVideoViewPlayerActivity.VIDEO_URL, menuUrl).navigation();
            }
        }
    }

    @Override
    protected void initData() {

        wvDetails.setVisibility(View.GONE);
        //url= GdproApplication.strH5Url+"";
        WebSettings settings = wvDetails.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setBlockNetworkImage(false);//解决图片不显示
        settings.setBuiltInZoomControls(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setUseWideViewPort(true);//扩大比例的缩放
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//启用地理定位，默认为true
        settings.setGeolocationEnabled(true);
        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        settings.setGeolocationDatabasePath(dir);

        settings.setDomStorageEnabled(true);
        wvDetails.requestFocus();
        settings.setSupportZoom(true); // 设置可以支持缩放

        String userAgentString = settings.getUserAgentString();
        settings.setUserAgentString(userAgentString + "/geekit");

        wvDetails.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http:") || !url.startsWith("https:")) {
                    if (url.startsWith("alipay")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse(url));
                        startActivity(intent);
                        return true;
                    } else {
                        return false;
                    }
                   /* Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(url));
                    startActivity(intent);
                    return true;*/
                }
                wvDetails.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }
        });
        wvDetails.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    pbLoading.setProgress(newProgress);
                    pbLoading.setVisibility(View.GONE);
                    wvDetails.setVisibility(View.VISIBLE);
                } else {
                    pbLoading.setProgress(newProgress);
                    pbLoading.setVisibility(View.VISIBLE);
                    wvDetails.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (type == 1) {
                    if (title.length() > 12) {
                        setTitle(title.substring(0, 10) + "...");
                    } else {
                        setTitle(title);
                    }
                }
            }
        });
        if (type == 1) {
            wvDetails.addJavascriptInterface(new JSInterface1(), "client");
        }
        wvDetails.loadUrl(url);
    }

    class JSInterface1 {
        //JavaScript调用此方法
        @JavascriptInterface
        public void jump(String id) {
            baseStartActivityWith("/mall/ProductDetailActivity")
                    .withString("id", id)
                    .withInt("level", 1)
                    .navigation();
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_webh5_loading;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvDetails.canGoBack()) {
            wvDetails.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

}
